class User {
  final String id;
  final String username;
  final String email;
  final String avatarUrl;
  final bool isLoggedIn;
  final DateTime? lastLoginTime;

  User({
    required this.id,
    required this.username,
    required this.email,
    required this.avatarUrl,
    required this.isLoggedIn,
    this.lastLoginTime,
  });

  factory User.empty() {
    return User(
      id: '',
      username: '',
      email: '',
      avatarUrl: '',
      isLoggedIn: false,
      lastLoginTime: null,
    );
  }

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['id'] ?? '',
      username: json['username'] ?? '',
      email: json['email'] ?? '',
      avatarUrl: json['avatarUrl'] ?? '',
      isLoggedIn: json['isLoggedIn'] ?? false,
      lastLoginTime: json['lastLoginTime'] != null
          ? DateTime.parse(json['lastLoginTime'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'email': email,
      'avatarUrl': avatarUrl,
      'isLoggedIn': isLoggedIn,
      'lastLoginTime': lastLoginTime?.toIso8601String(),
    };
  }

  User copyWith({
    String? id,
    String? username,
    String? email,
    String? avatarUrl,
    bool? isLoggedIn,
    DateTime? lastLoginTime,
  }) {
    return User(
      id: id ?? this.id,
      username: username ?? this.username,
      email: email ?? this.email,
      avatarUrl: avatarUrl ?? this.avatarUrl,
      isLoggedIn: isLoggedIn ?? this.isLoggedIn,
      lastLoginTime: lastLoginTime ?? this.lastLoginTime,
    );
  }

  @override
  String toString() {
    return 'User{id: $id, username: $username, email: $email, isLoggedIn: $isLoggedIn}';
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is User &&
          runtimeType == other.runtimeType &&
          id == other.id;

  @override
  int get hashCode => id.hashCode;
}

